package edu.chinasoft.servlet;

import edu.chinasoft.domain.Function;
import edu.chinasoft.domain.User;
import edu.chinasoft.service.FunctionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet(name = "myFindFunctionByUserServlet",value = "/servlet/findfunctionbyuserservlet")
public class FindFunctionByUserServlet extends HttpServlet {
    private static Logger logger = LoggerFactory.getLogger(FindFunctionByUserServlet.class);
    private FunctionService functionService;

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info(" FindFunctionByUserServlet >doPost start.......");
        this.doGet(request, response);
        logger.info(" FindFunctionByUserServlet >doPost end.......");
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info(" FindFunctionByUserServlet >doGet start.......");
        WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        FunctionService functionServiceImp = wac.getBean("functionService", FunctionService.class);
        this.functionService = functionServiceImp;

        HttpSession session = request.getSession(false);
        User userLogin = (User) session.getAttribute("userLogin");

        if(userLogin==null){//没有用户登录
            response.sendRedirect(request.getContextPath()+"/leftNoUser.jsp?key01="+new Date().getTime());
        }else{
            List<Function> functionList = functionService.selectFunctionByUserID(userLogin);
            session.setAttribute("Function",functionList);

            response.sendRedirect(request.getContextPath()+"/left.jsp?key01="+new Date().getTime());

        }

        logger.info(" FindFunctionByUserServlet >doGet end.......");
    }
}
